/**
 * @desc  所有的字典都来之系统设置
 * */
 
import * as  dictionaryValueApi from '@/apis/sysDictionaryValue.api.js'
import cookie from "js-cookie"




function safeParse(str) {
  try {
    return JSON.parse(str);
  } catch (e) {
    console.error(e)
    return null
  }
  
}

export const ajaxDicList = function (code) {
  if (!code) {
    return null;
  }
  var uuidKey = `dic.${code}`;
  var codeInCookie = cookie.get(uuidKey);
  if (codeInCookie && sessionStorage.getItem(uuidKey)) {
    return Promise.resolve(safeParse(sessionStorage.getItem(uuidKey)))
  }
  return dictionaryValueApi.getvaluelistbydiscode(code).then(res => res.data).then(datalist => {


//不缓存
    if (!datalist) {
      return Promise.resolve([])
    }


//不缓存
    if (datalist && datalist.length <= 0) {
      return Promise.resolve([])
    }
    
    
    
    
    datalist = datalist.map(item => {
      return {
        label: item.name,
        value: item.code + ""
      }
    })
    
    /*dic code  存储 半个小时*/
    cookie.set(uuidKey, 1, {expires: .5 / 24})
    sessionStorage.setItem(uuidKey, JSON.stringify(datalist))
    return datalist
  })
}



export function ToFixed (value, params) {
  if (!value) return '';
  if(typeof value=='string') return value;
  if (!params) {
    params = 2;
  }
  return value.toFixed(params);
}



